import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {visualizer} from "rollup-plugin-visualizer";

// https://vitejs.dev/config/
export default defineConfig({
    base: './', // 设置生产环境的基本路径
    esbuild: {
        pure: ['console.log'], // 删除 console.log
        drop: ['debugger'], // 删除 debugger
    },
    plugins: [
        visualizer({
            emitFile: false,
            filename: 'stats.html',
            open: true,
            sourcemap: true
        }),
        vue()
    ],
    resolve: {
        alias: {
            '@': '/src'
        }
    },
    //跨域配置
    server: {
        //端口
        port: 8080,
        proxy: {
            '/api': {
                target: `http://localhost:80`,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, '/api')
            }
        }
    },
    build: {
        rollupOptions: {
            output: {
                sourcemap: true,
                chunkFileNames: 'static/js/[name]-[hash].js',
                entryFileNames: 'static/js/[name]-[hash].js',
                assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
                manualChunks(id) {
                    // id为文件的绝对路径
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString()
                    }
                }
            }
        }
    }
})
